
package canglang.scan.user.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import canglang.scan.core.base.action.BaseAction;
import canglang.scan.core.base.model.LabelValue;
import canglang.scan.core.base.pagination.PaginatedListHelper;
import canglang.scan.core.base.properties.AppDefaultProperty;
import canglang.scan.core.base.util.StringUtil;
import canglang.scan.core.base.util.DateUtil;
import canglang.scan.user.model.UserModel;
import canglang.scan.user.service.UserManager;
import canglang.scan.user.to.SearchTO;

import com.opensymphony.xwork2.util.CreateIfNull;
import com.opensymphony.xwork2.util.Element;

/**
 * 
 */

@SuppressWarnings("unchecked")
public class UserAction extends BaseAction {

    private static final long serialVersionUID = -8523460645995972276L;

    private UserModel userModel;

    private PaginatedListHelper helper;

    private SearchTO searchTO;

    private UserManager userManager;

    /**
     * @return the roleList
     */
    public List getRoleList() {
        List list=new ArrayList();
        String admin=this.getText("label.admin");
        String normalUser=this.getText("label.user");
        String operator=this.getText("label.operator");
        LabelValue l1=new LabelValue(admin,"A");
        LabelValue l2=new LabelValue(normalUser,"U");
        LabelValue l3=new LabelValue(operator,"O");
        list.add(l1);
        list.add(l2);
        list.add(l3);
        return list;
    }

    /**
     * @return the userModel
     */
    public UserModel getUserModel () {
        return userModel;
    }

    /**
     * @param userModel the userModel to set
     */
    public void setUserModel (UserModel userModel) {
        this.userModel = userModel;
    }

    /**
     * @return the helper
     */
    public PaginatedListHelper getHelper () {
        return helper;
    }

    /**
     * @param helper the helper to set
     */
    public void setHelper (PaginatedListHelper helper) {
        this.helper = helper;
    }

    public SearchTO getSearchTO() {
        return searchTO;
    }

    public void setSearchTO(SearchTO searchTO) {
        this.searchTO = searchTO;
    }

    /**
     * @param userManager the userManager to set
     */
    public void setUserManager (UserManager userManager) {
        this.userManager = userManager;
    }

    @Element(value = UserModel.class)
    @CreateIfNull(value = true)
    public List<Object> getResultList () {
        return helper != null ? helper.getList() : null;
    }

    public void setResultList (List<Object> alist) {
        if (helper == null)
            helper = new PaginatedListHelper();
        helper.setList(alist);
    }

    public String searchUser () {
       if (searchTO == null) {
            searchTO = new SearchTO();
        }
        if (helper == null) {
            helper = new PaginatedListHelper();
            helper.setSortCriterion("USER_ID");
        }
        helper = userManager.search(helper, searchTO);
        return SUCCESS;
    }



    public String addUser () {
        UserModel user = userManager.getUser(userModel.getUserId());
        if(user!=null){
            this.addActionError(AppDefaultProperty.getProperty("label.uesr.already.exist"));
            return INPUT;
        }
        UserModel cUser = (UserModel) this.getSession().getAttribute("user");
        userModel.setCreateId(cUser.getUserId());
        userModel.setUpdateId(cUser.getUserId());
        Date date = DateUtil.getDate();
        userModel.setCreateDt(date);
        userModel.setUpdateDt(date);
        userManager.addUser(userModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.add.successful"));
        return SUCCESS;
    }

    public String viewUser () {
        userModel = userManager.getUser(userModel.getSelectId());
        return SUCCESS;
    }

    public String saveUser () {
        UserModel cUser = (UserModel) this.getSession().getAttribute("user");
        userModel.setUpdateId(cUser.getUserId());
        userModel.setUpdateDt(DateUtil.getDate());
        userModel = userManager.saveUser(userModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.save.successful"));
        return SUCCESS;
    }

    public String deleteUser () {
        for (int i = 0; i < helper.getList().size(); i++) {
            UserModel u = (UserModel) helper.getList().get(i);
            if (u != null && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("")&& !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("false")) {
                    userManager.deleteUser(u.getSelectId());
            }
        }
        this.saveMessage(AppDefaultProperty.getProperty("label.delete.successful"));  
        return searchUser();
    }
    

    

}
